\section{ADC Operations}


The ADC operations are fortunately fairly simple and similar to what was done in the previous lab. The major expansion is that the ADC now returns a uint16t data type as we stack both the ADCL and ADCH registers into the returned data. This provides 10 bit precision ADC conversion as opposed to the 8 bit previously used. How big a difference this actually makes is probably not very significant, but the voltage being measured at the ADC is stepped-down from the amplifier, and the calculation will be more sensitive to error because of that. This is why we want to minimize error at the ADC, at the expense of a little complexity.

\begin{lstlisting}
uint16_t adconvert(int adcx){
	uint16_t adclh = 0;	//ADC return value
	int hold;
	//Set internal 1.1v VRef with external capacitor req'd at AREF
	ADMUX = 0b1100000;
	if(adcx < 8) ADMUX |=adcx; //If adcx >7, other bits will be screwed up.
	else return(-1);	//Return -1 if failed due to poor adcx select
	
	//The following actually does the conversion
	PRR &=~(1<<PRADC);
	ADCSRA |=(1<<ADSC);	//ADC Start conversion bit
	while(ADCSRA&_BV(ADSC)); //Wait for A/D Conversion
	//BV does bit value
	
	//ADCL should be read first, reading ADCH re-enables writing to
	//the ADC output registers, so it is done last for safety
	
	hold = ADCL;o
	adclh |= ADCH;	//Should OR to bottom 8 bits of adclh
	adclh = adclh<<8; //Shift ADCH to top 8 bits
	adclh |= hold;	//Replace bottom 8 bits with ADCL
	
	return(adclh);	//return 10 bit ADC in 16 bit signed INT
}
\end{lstlisting}

Voltage and current meters function on different ADC's (voltage measurements work on ADC0, while current measurements are on ADC1) because this is where the differential amplifiers are connected. The ADC to be used is set at compile time, but is easily changeable by altering the function call to adconvert(); The function takes an integer between 0-8, which represents which ADC to use in making conversion.

For both unit types, the mux is set up to set the ADC to use the internal 1.1v reference voltage with the external capacitor. Voltage readings are designed to be scaled to this value (after experimental testing and calibrating the output.) Errors in the output of the differential amplifiers are going to create errors, but depending on the exact degree of voltage scaling, the reference scale quantity will need to be adjusted (this is declared as a variable in the main function so adjustments can be made through the whole program.)